﻿Ext.define('app.ux.UserSelector', {
    extend: 'Ext.panel.Panel',
    border: true,
    layout: 'fit',
    tbar: [],
    requires: ['app.ux.ItemSelector'],
    initComponent: function () {
        var me = this;

        me.items = [
           {
               xtype: 'container',
               layout: 'hbox',
               anchor: '100%',
               items: [
                   me.getOrganization(),
                   me.getUserSelector()
               ]
           }
        ];

        var txt_account = new Ext.form.field.Text({ emptyText: '查询用户', width: 200 });
        me.tbar = [
            "->",
            txt_account,
            {
                text: '查询', handler: function () {
                    var userName = txt_account.getValue();
                    if (userName != "") {
                        //var chk = me.organization.getSelection()[0];
                        //console.log(chk);
                        var oId = null;
                        //if (chk) {
                        //    oId = chk.data.id;
                        //}
                        me.getUserSelector().getStore().removeAll();
                        me.getUserSelector().store.reload({ params: { id: oId, userName: userName } });
                    }
                }
            }
        ];
        me.callParent();
    },
    //listeners: {
    //    afterrender: function (me) {
    //        App.AjaxGet('/api/services/app/organizationUnit/GetOrganizationTree', function (data) {
    //            me.getOrganization().store.loadData(data.result);
    //        });
    //    }
    //},
    getOrganization: function () {
        var me = this;
        if (!this.organization) {

            var store = new Ext.data.TreeStore({
                autoLoad: true,
                nodeParam: 'parentId',
                // 根节点的参数值是0
                defaultRootId: "0",
                //{ "key": "1", "pId": "0", "text": "administrator", "leaf": false, "children": [{ "key": "2", "pId": "1", "text": "detp", "leaf": true, "children": [] }] }
                proxy: {
                    // 请求方式
                    type: 'ajax',
                    // 请求网址
                    url: '/api/services/app/business/GetCompanyLevel?parentId=0',
                    reader: {
                        type: 'json',
                        rootProperty: function (node) {
                            return node.result || node.children;
                        }
                    }
                }
            });

            this.organization = new Ext.tree.Panel({
                flex: 1,
                height: '100%',
                border: false,
                title: '所属科室',
                useArrows: true,
                rootVisible: false,
                store: store,
                root: { id: '0', text: '' },
                listeners: {
                    itemclick: function (tree, record, item, index, e, eOpts) {
                        me.getUserSelector().store.load({ params: { businessId: record.data.businessId, laboratoryId: record.data.laboratoryId, rdCenterId: record.data.rdCenterId } });
                    }
                }
            });
        }
        return this.organization;
    },
    getUserSelector: function () {
        if (!this.userSelector) {
            this.userSelector = new app.ux.ItemSelector({
                name: 'itemselector',
                flex: 2,
                height: '100%',
                //hideNavIcons: true,
                imagePath: '/Content/ExtJs/itemselector/',
                store: new Ext.data.JsonStore({
                    autoLoad: false,
                    fields: ['key', 'value'],
                    proxy: {
                        // 请求方式
                        type: 'ajax',
                        // 请求网址
                        url: '/api/services/app/business/GetUsers',
                        reader: {
                            type: 'json',
                            rootProperty: 'result'
                        }
                    }
                }),
                displayField: 'value',
                valueField: 'key',
                //value: ['3', '4', '6'],
                //allowBlank: false,
                msgTarget: 'side',
                fromTitle: '可选用户',
                toTitle: '已选用户'
            });
        }
        return this.userSelector;
    },
    getValue: function () {
        var users = this.getUserSelector().getValue();
        return users;
    }
})